package com.zhanghuang.springbootstudy01.configuration;

import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.sql.DataSource;
import java.util.HashMap;


@Configuration
public class DruidDataSourceConfiguration {

    //配置 StatViewServlet
    // https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE
    @Bean
    public ServletRegistrationBean getStatViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        HashMap<String, String> map = new HashMap<>();

        //后台的登录用户名和密码
        map.put("loginUsername", "admin");
        map.put("loginPassword", "admin");
        // 访问权限
        // map.put("allow","localhost"); //只允许本机访问
        map.put("allow", ""); // 所有人都可以访问
        // 关闭监控页面的重置功能
        map.put("resetEnable","false");
        // deny拒绝访问
        // map.put("deny","192.168.1.1"); // ip会被拒绝访问

        servletRegistrationBean.setInitParameters(map); //设置servlet的初始化参数
        return servletRegistrationBean;
    }

    public FilterRegistrationBean getWebStatFilter(){
        FilterRegistrationBean<Filter> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(new WebStatFilter());
        HashMap<String,String> map = new HashMap<>();
        map.put("exclusions","*.js,*.css,/druid/*");
        filterBean.setInitParameters(map);
        return filterBean;

    }
}
